import gui.DatabaseView;
import gui.GrafiekView;
import gui.MaandView;
import gui.MeetgegevensView;
import gui.MeetstationView;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;

import model.Model;

@SuppressWarnings("serial")
public class WeerApp extends JFrame {

	// Tabs
	private JTabbedPane tabs;

	private MaandView maandView;

	private GrafiekView grafiekView;

	private MeetstationView meetstationView;

	private MeetgegevensView meetgegevensView;

	private DatabaseView databaseView;

	// Menu
	private JMenuBar menuBar;

	private JMenu menu;

	private JMenuItem screenshot;

	private JMenuItem exit;

	public WeerApp() {
		setLayout(new BorderLayout());

		// Maak een nieuw model aan
		Model model = new Model();

		// Maak de tabs aan
		tabs = new JTabbedPane();
		maandView = new MaandView();
		grafiekView = new GrafiekView();
		meetstationView = new MeetstationView(model);
		meetgegevensView = new MeetgegevensView(model);
		databaseView = new DatabaseView(model);

		// Voeg de tabs toe
		tabs.addTab("Maandoverzicht", maandView);
		tabs.addTab("Grafiek", grafiekView);
		tabs.addTab("Meetstations", meetstationView);
		tabs.addTab("Meetgegevens", meetgegevensView);
		tabs.addTab("Database", databaseView);

		add(tabs, BorderLayout.CENTER);

		// Maak het menu aan
		menuBar = new JMenuBar();
		menu = new JMenu("Menu");
		menu.setMnemonic(KeyEvent.VK_M);
		menuBar.add(menu);
		screenshot = new JMenuItem("Screenshot");
		screenshot.setMnemonic(KeyEvent.VK_S);
		screenshot.addActionListener(new ScreenshotHandler());
		exit = new JMenuItem("Exit");
		exit.setMnemonic(KeyEvent.VK_E);
		exit.addActionListener(new ExitHandler());
		menu.add(screenshot);
		menu.add(exit);

		add(menuBar, BorderLayout.NORTH);

	}

	public static void main(String args[]) {
		WeerApp app = new WeerApp();
		app.setDefaultCloseOperation(EXIT_ON_CLOSE);
		app.setBounds(50, 50, 700, 600);
		app.setTitle("Weerapplicatie");
		app.setVisible(true);
	}

	private class ExitHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	}

	private class ScreenshotHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			try {
				Robot robot = new Robot();
				/* new */
				Rectangle screen = new Rectangle(getX(), getY(), getWidth(),
						getHeight());
				String tab = tabs.getTitleAt(tabs.getSelectedIndex());
				if (tab.equals("Maandoverzicht")) {
					Rectangle tabje = maandView.getRectangleBounds();
					screen.setRect(getX() + tabje.getX(), getY() + tabje.getY()
							+ menuBar.getHeight(), tabje.getWidth(), tabje
							.getHeight());
				} else if (tab.equals("Grafiek")) {
					Rectangle tabje = grafiekView.getRectangleBounds();
					screen.setRect(getX() + tabje.getX(), getY() + tabje.getY()
							+ menuBar.getHeight(), tabje.getWidth(), tabje
							.getHeight());
				}
				BufferedImage screenshot = robot.createScreenCapture(screen);
				ImageIO.write(screenshot, "JPG", new File(getFilename()));
				/* old */
			} catch (AWTException exc) {
				System.out.println("Ohoh");
			} catch (IOException ex) {
				System.out.println("Oops");
			}
		}

		public String getFilename() {
			String filename = "";
			String tab = tabs.getTitleAt(tabs.getSelectedIndex());
			if (tab.equals("Maandoverzicht")) {
				filename += "Maandoverzicht";
				if (maandView.getBoxPanel().getName() != null) {
					filename += maandView.getBoxPanel().getName();
				}
			} else if (tab.equals("Grafiek")) {
				filename += "Grafiek";
				if (grafiekView.getGrafiekBox().getName() != null) {
					filename += grafiekView.getGrafiekBox().getName();
				}
			}
			return filename;
		}
	}
}
